package com.zenchn.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 *AOP切面异步线程池 组件 注册配置
 * @author panbingqi
 * @date 2021-06-01
 */
@Configuration
@ComponentScan({"com.zenchn.aspect.service","com.zenchn.service.impl"})
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Value("${spring.async.core-pool-size}")
    private Integer corePoolSize;
    @Value("${spring.async.max-pool-size}")
    private Integer maxPoolSize;
    @Value("${spring.async.queue-capacity}")
    private Integer queueCapacity;
    @Value("${spring.async.keep-alive-seconds}")
    private Integer keepAliveSeconds;
    @Value("${spring.async.thread-name-prefix}")
    private String threadNamePrefix;




    @Override
    public Executor getAsyncExecutor() {
        //线程池
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(corePoolSize);
        taskExecutor.setMaxPoolSize(maxPoolSize);
        taskExecutor.setQueueCapacity(queueCapacity);
        taskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        taskExecutor.setThreadNamePrefix(threadNamePrefix);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}
